package com.my.ads.lib.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.example.cleantest.SecondActivity;
import com.rapid.clean.deep.R;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by xuanyang on 2018/3/8.
 */

public class IconActive {


    public static String getFromSp(Context context, String spName, String key, String defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_PRIVATE);
        return sp.getString(key, defaultValue);
    }

    /**
     * 添加当前应用的桌面快捷方式
     */
    public static void addShortcut(Context context, int appIcon) {
//        Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//        //设置点击快捷方式时启动的Activity,因为是从Lanucher中启动，所以包名类名要写全。
//        Log.i(Config.TagLog, ThirdActivity.class.getName());
//        shortcutIntent.setComponent(new ComponentName(context.getPackageName(), "com.example.cleantest.ThirdActivity"));
//        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK);
//        Intent resultIntent = new Intent();
//        //设置快捷方式图标
//        resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
//                Intent.ShortcutIconResource.fromContext(context,
//                        appIcon)); // 没有过期的方法是什么？
//        //启动的Intent
//        resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
//        //设置快捷方式的名称
//        resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Clear");
//        resultIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//        context.sendBroadcast(resultIntent);


        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Clear");
        shortcut.putExtra("duplicate", false); //不允许重复创建
//指定当前的Activity为快捷方式启动的对象: 如
//com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须加上点号(.)，否则快捷方式无法启动相应程序
        ComponentName comp = new ComponentName(context.getPackageName(), "com.example.cleantest.ThirdActivity");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
//快捷方式的图标
        Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, appIcon);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
        context.sendBroadcast(shortcut);



    }


    //生成常驻通知栏
    public static void sendNotification(Context context) {


        try {
            // 使用RemoteViews来定义通知栏的界面
            RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.layout_notification);
            // 设置垃圾大小
//        contentView.setTextViewText(R.id.text_m, (int) ((Math.random() * 2 + 1) * 100) + "M");
            Intent notificationIntent = new Intent(context, SecondActivity.class);
//            ComponentName comp = new ComponentName(context.getPackageName(), "com.example.cleantest.ThirdActivity");
//            notificationIntent.setComponent(comp);
            // 携带的数据
            PendingIntent contentIntent = PendingIntent.getActivity(context, 1, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            // 组件跳转
            contentView.setOnClickPendingIntent(R.id.btn_clean, contentIntent);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setContentIntent(contentIntent);
            builder.setContent(contentView);
            NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            Notification no = builder.build();
            no.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻 Flag
            // no.flags =  Notification.FLAG_AUTO_CANCEL;
            nm.notify(233, no); // 可以更换一下
        } catch (Exception e) {
            e.printStackTrace();
        }


    }




}
